/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.service.impl;

import com.bjboot.caocao.common.utils.BuildTreeUtil;
import com.bjboot.caocao.system.dao.DeptMapper;
import com.bjboot.caocao.system.domain.DeptDO;
import com.bjboot.caocao.system.domain.Tree;
import com.bjboot.caocao.system.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Description DeptServices实现类
 *@Author 操文健
 *@Date 2018-09-12 上午11:38
 *@Version 1.0
 **/
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;


    @Override
    public DeptDO get(Long deptId) {
        return deptMapper.selectByPrimaryKey(deptId);
    }

    @Override
    public List<DeptDO> list(Map<String, Object> map) {
        return deptMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return deptMapper.count(map);
    }

    @Override
    public int save(DeptDO sysDept) {
        return deptMapper.insertSelective(sysDept);
    }

    @Override
    public int update(DeptDO sysDept) {
        return deptMapper.updateByPrimaryKeySelective(sysDept);
    }

    @Override
    public int remove(Long deptId) {
        return deptMapper.deleteByPrimaryKey(deptId);
    }

    @Override
    public int batchRemove(Long[] deptIds) {
        return deptMapper.batchRemove(deptIds);
    }

    @Override
    public Tree<DeptDO> getTree() {
        List<Tree<DeptDO>> trees = new ArrayList<Tree<DeptDO>>();
        List<DeptDO> sysDepts = deptMapper.list(new HashMap<String,Object>(16));
        for (DeptDO sysDept : sysDepts) {
            Tree<DeptDO> tree = new Tree<DeptDO>();
            tree.setId(sysDept.getDeptId().toString());
            tree.setParentId(sysDept.getParentId().toString());
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<DeptDO> t = BuildTreeUtil.build(trees);
        return t;
    }

    /**
     * 查看该部门下是否有用户
     * @param deptId
     * @return
     */
    @Override
    public boolean checkDeptHasUser(Long deptId) {
        // 首先查询该部门，是否存在用户
        int result = deptMapper.getDeptUserNumber(deptId);
        if (result != 0) {
            return false;
        }

        List<DeptDO> rtnDeptList = queryAllDeptsByParentId(deptId);
        for (DeptDO deptDO : rtnDeptList) {
            result = deptMapper.getDeptUserNumber(deptDO.getDeptId());
            if (result != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 递归查询部门Id的子部门
     * @param deptId
     * @return
     */
    private List<DeptDO> queryAllDeptsByParentId (Long deptId) {

        List<DeptDO> rtnDeptList = new ArrayList<DeptDO>();

        // 组装参数信息
        Map<String, Object> paraMap = new HashMap<>(16);
        paraMap.put("parentId", deptId);
        List<DeptDO> qryList = deptMapper.list(paraMap);
        if (StringUtils.isEmpty(qryList)) {
            return rtnDeptList;
        } else {
            for (DeptDO deptDOTmp : qryList) {
                rtnDeptList.addAll(queryAllDeptsByParentId(deptDOTmp.getDeptId()));
            }
        }
        return rtnDeptList;
    }
}
